﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Diagnostics;
using BookingEngine.Data.Fog;
using Gizmox.WebGUI.Forms;

namespace BookingEngine.Data
{
    public static class HotelSearchService
    {
        private static readonly loginRQ s_LoginZh;
        private static readonly loginRQ s_LoginEn;
        public static readonly List<string> RateCodes;

        static HotelSearchService()
        {
            s_LoginZh = new loginRQ
                            {
                                user = ConfigurationManager.AppSettings["FogUserName"],
                                key = ConfigurationManager.AppSettings["FogPassword"],
                                lang = "zh",
                            };
            s_LoginEn = new loginRQ
                            {
                                user = ConfigurationManager.AppSettings["FogUserName"],
                                key = ConfigurationManager.AppSettings["FogPassword"],
                                lang = "en",
                            };
            RateCodes = new List<string>
                            {
                                "BAR",
                                "BARB",
                                "OSBVR",
                                "RACK",
                                "WPRO1",
                                "WPRO2",
                                "WPRO3",
                                "WPRO4",
                                "WPRO5",
                                "WPRO6",
                                "WPRO7",
                                "WPRO8",
                                "JJWTR",
                                "BEPRO1"
                            };
        }

        #region 数据查询

        /// <summary>
        /// </summary>
        /// <param name="language"> 语言种类(支持中英日文)，‘zh’,’en’,’jp’ </param>
        /// <param name="checkInDate"> 入住日期 </param>
        /// <param name="checkOutDate"> 离店日期 </param>
        /// <param name="hotelCode"> FOG提供的酒店ID </param>
        /// <returns> </returns>
        public static validHotelDetail GetHotel(string language, DateTime checkInDate, DateTime checkOutDate, string hotelCode)
        {
            if (string.IsNullOrEmpty(hotelCode))
            {
                throw new ArgumentNullException("hotelCode");
            }
            try
            {
                using (var serviceClient = new HotelSearchWebServiceClient())
                {
                    var validHotelDetailRS = serviceClient.getValidHotelByHotelId(language == "en" ? s_LoginEn : s_LoginZh, checkInDate.Date.ToString("yyyy-MM-dd"), checkOutDate.Date.ToString("yyyy-MM-dd"), hotelCode, "", "T", 1.0);
                    if (validHotelDetailRS.errorRS == null && validHotelDetailRS.validHotelDetail != null)
                    {
                        return validHotelDetailRS.validHotelDetail;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //Debug.WriteLine(ex.Message);
            }
            return null;
        }

        /// <summary>
        /// </summary>
        /// <param name="language"> </param>
        /// <param name="hotelCode"> </param>
        /// <returns> </returns>
        public static Dictionary<int, string> GetHotelRateCodes(string language, string hotelCode)
        {
            if (string.IsNullOrEmpty(hotelCode))
            {
                throw new ArgumentNullException("hotelCode");
            }
            try
            {
                using (var serviceClient = new HotelSearchWebServiceClient())
                {
                    var planCodeRS = serviceClient.getPlanCodeOrderBySeq(language == "en" ? s_LoginEn : s_LoginZh, hotelCode);
                    if (planCodeRS.errorRS == null && planCodeRS.ratecodes != null)
                    {
                        var rateCodes = planCodeRS.ratecodes;
                        var rateCodesDictionary = new Dictionary<int, string>();
                        for (var index = 0; index < rateCodes.Length; index++)
                        {
                            rateCodesDictionary.Add(index, rateCodes[index]);
                        }
                        return rateCodesDictionary;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //Debug.WriteLine(ex.Message);
            }
            return null;
        }

        /// <summary>
        /// </summary>
        /// <param name="language"> 语言种类(支持中英日文)，‘zh’,’en’,’jp’ </param>
        /// <param name="checkInDate"> 入住日期 </param>
        /// <param name="checkOutDate"> 离店日期 </param>
        /// <param name="hotelCode"> FOG提供的酒店ID </param>
        /// <returns> </returns>
        public static validHotelDetail GetHotelSimple(string language, DateTime checkInDate, DateTime checkOutDate, string hotelCode)
        {
            if (string.IsNullOrEmpty(hotelCode))
            {
                throw new ArgumentNullException("hotelCode");
            }
            try
            {
                using (var serviceClient = new HotelSearchWebServiceClient())
                {
                    var validHotelDetailRS = serviceClient.getValidHotelByHotelId(language == "en" ? s_LoginEn : s_LoginZh, checkInDate.Date.ToString("yyyy-MM-dd"), checkOutDate.Date.ToString("yyyy-MM-dd"), hotelCode, "", "F", 1.0);
                    if (validHotelDetailRS.errorRS == null && validHotelDetailRS.validHotelDetail != null)
                    {
                        return validHotelDetailRS.validHotelDetail;
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                //Debug.WriteLine(ex.Message);
            }
            return null;
        }

        #endregion
    }
}